
import 'dart:convert';

import 'package:encrypt/encrypt.dart';
import 'package:xreader/logs/log.dart';

RegExp _regExpA = new RegExp("[-]");
RegExp _regExpB = new RegExp("[@]");
RegExp _regExpC = new RegExp("[_]");
String _yds = "1234567890qazxsw";
class SecureText{


  static String _converSafeBase64UrlToOriginBase64(String safeBase64Url) {
    if(safeBase64Url == ""){
      return safeBase64Url;
    }
    String  originBase64 = safeBase64Url.replaceAll(_regExpA,"+");
    originBase64 = originBase64.replaceAll(_regExpB, "=");
    originBase64 = originBase64.replaceAll(_regExpC, "/");
    return originBase64;
  }

  static String _AesDecrypt(String data,String keyStr,String ivStr){
    Key key  = Key.fromUtf8(keyStr);
    IV iv = IV.fromUtf8(ivStr);
    Encrypter encrypter = Encrypter(AES(key,mode: AESMode.cbc));
    return encrypter.decrypt64(data,iv: iv);
  }

  static String AesDecryptFromSafeBase64Url(String data){
    var base64Str = _converSafeBase64UrlToOriginBase64(data);
    return _AesDecrypt(base64Str,_yds,_yds);
  }

  static Map<String,dynamic> aesDecrypt(String? data){
    Map<String,dynamic> result = Map();
    if(data == null || data.isEmpty){
      return result;
    }
    try{
      var decryptText = AesDecryptFromSafeBase64Url(data);
      //Log.i("decryptText==》$decryptText");
      result = jsonDecode(decryptText);
      //Log.i("result==》$result");
    }catch(e){
      Log.i("error===>$e");
      return result;
    }
    return result;
  }


}